<?php

namespace app\payment;

use app\services\ConfigService;
use Yansongda\Pay\Pay;
use function EasyWeChat\Kernel\Support\get_client_ip;

class WechatPay extends AbstractPay
{
    public $supportPayWay = [
        'h5',
        'mp',
        'app',
        'mini',
        'scan'
    ];

    public function pay()
    {
        Pay::config(ConfigService::getPaymentConfig(PayFactory::WECHAT, $this->data['pay_way_type']));

        $data = [
            'out_trade_no' => $this->data['order_no'],
            'description' => '支付商品',
            'amount' => [
                'total' => $this->data['total_amount'] * 100,
                'currency' => 'CNY'
            ],

        ];

        switch ($this->data['pay_way_type']) {
            case 'scan':
                return Pay::wechat()->scan($data);
                break;
            default:
            case 'mini':
                $data['payer'] = [
                    'openid' => $this->data['openid']
                ];
                return Pay::wechat()->mini($data);
                break;
            case 'mp':
                $data['payer'] = [
                    'openid' => $this->data['openid']
                ];
                return Pay::wechat()->mp($data);
                break;
            case 'h5':
                $data['scene_info'] = [
                    'payer_client_ip' => get_client_ip(),
                    'h5_info' => [
                        'type' => 'wap'
                    ]
                ];
                return Pay::wechat()->wap($data);
                break;
            case 'app':
                return Pay::wechat()->app($data);
                break;
        }
    }

    public function payBack()
    {
        Pay::config(ConfigService::getPaymentConfig(PayFactory::WECHAT));

        return Pay::wechat()->refund([
            'out_trade_no' => $this->data['order_no'],
            'out_refund_no' => $this->data['order_no'] . '_refund',
            'amount' => [
                'total' => $this->data['total_amount'] * 100,
                'refund' => $this->data['total_amount'] * 100,
                'currency' => 'CNY'
            ]
        ]);
    }
}